package com.cats.template.sys.config.authority;

import com.cats.template.sys.pojo.model.PermissionModel;
import com.cats.template.sys.services.PermissionService;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 * @author duxiaobo
 * @date 2021/9/272:38 下午
 */
//@Configuration
public class ShiroRuleConfig {

    @Autowired
    private PermissionService permissionService;

    @Value("${shiro.loginUrl}")
    private String loginUrl;

    @Value("${shiro.unauthorizedUrl}")
    private String unauthorizedUrl;

    @Bean(name = "filterMap")
    public Map<String, String> defaultShiroFilterChainDefinition(ShiroFilterFactoryBean shiroFilter) {
        Map<String, String> filterRuleMap = new LinkedHashMap<>();
        shiroFilter.setLoginUrl(loginUrl);
        shiroFilter.setUnauthorizedUrl(unauthorizedUrl);
        Set<PermissionModel> list = permissionService.getAll();
        list.forEach(permissionModel -> filterRuleMap.put("/template" + permissionModel.getUrl(), "perms[" + permissionModel.getPerms() + "]"));
        filterRuleMap.put("/**", "authc");
        return filterRuleMap;
    }
}
